<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">还款类型</label>
        <el-input v-model="query.repayType" clearable placeholder="还款类型" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">状态</label>
        <el-select v-model="query.repayStatus" style="width: 185px;"><el-option v-for="item in status" :key="item.key" :value="item.value" :label="item.label" /></el-select>
        <!-- <el-input v-model="query.repayStatus" clearable placeholder="状态" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">

        <span slot="right">
          <div style="display: inline-block;margin: 0px 2px;">
            <el-upload
              ref="upload"
              :show-file-list="false"
              :before-upload="beforeupload"
              :on-success="handleSuccessFile"
              :on-error="handleErrorFile"
              :action="baseApi + '/api/repay/upload'"
              :headers="headers"
              accept=".xls,.xlsx"
              enctype="multipart/form-data"
            >
              <el-button type="primary" class="filter-item" size="mini" icon="el-icon-upload">导入</el-button>
            </el-upload>
          </div>
          <el-button
            size="mini"
            class="filter-item"
            type="text"
            style="background:#fffffb;"
            @click="downloadModel"
          >
            <svg-icon icon-class="log" />
            下载模板</el-button>
        </span>

      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <!-- <el-form-item label="还款类型">
            <el-input v-model="form.repayType" style="width: 370px;" />
          </el-form-item> -->
          <el-form-item label="还款期号">
            <el-input v-model="form.repayIssue" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="还款日期">
            <el-date-picker v-model="form.repayDate" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="还款总额">
            <el-input v-model="form.repayPrice" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="还款本金">
            <el-input v-model="form.repayPrincipal" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="还款利息">
            <el-input v-model="form.repayInterest" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="form.repayStatus" style="width: 370px;"><el-option v-for="item in status" :key="item.key" :value="item.value" :label="item.label" /></el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" show-summary @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <!-- <el-table-column prop="repayType" label="还款类型" /> -->
        <el-table-column prop="repayIssue" label="还款期号" align="center" />
        <el-table-column prop="repayDate" label="还款日期" align="center" />
        <el-table-column prop="repayPrice" label="还款总额" align="center" />
        <el-table-column prop="repayPrincipal" label="还款本金" align="center" />
        <el-table-column prop="repayInterest" label="还款利息" align="center" />
        <el-table-column prop="repayStatus" label="状态" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.repayStatus==='1'" type="success"> 已还</el-tag>
            <el-tag v-else type="warning"> 待还</el-tag>

          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.repayStatus"
              active-value="1"
              inactive-value="0"
              @change="isRepay(scope.row)"
            /></template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','repay:edit','repay:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      {{ ceshidata }}
    </div>
  </div>
</template>

<script>
import crudRepay from '@/api/repay'
import CRUD, { presenter, header, form, crud, ceshi } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
import { downloadFile } from '@/utils/index'
import { download } from '@/api/data'

const defaultForm = { id: null, repayType: null, repayIssue: null, repayDate: null, repayPrice: null, repayPrincipal: null, repayInterest: null, repayStatus: null }
export default {
  name: 'Repay',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud(), ceshi()],
  cruds() {
    return CRUD({ title: 'repay', url: 'api/repay', idField: 'id', sort: 'repayIssue,asc', crudMethod: { ...crudRepay }})
  },
  data() {
    return {
      status: [{ id: '1', label: '已还', value: '1' }, { id: '0', label: '待还', value: '0' }],
      headers: { 'Authorization': getToken() },
      permission: {
        add: ['admin', 'repay:add'],
        edit: ['admin', 'repay:edit'],
        del: ['admin', 'repay:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'repayType', display_name: '还款类型' },
        { key: 'repayStatus', display_name: '状态' }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'baseApi',
      'fileUploadApi'
    ])
  },
  mounted() {
    console.log()
  },
  methods: {
    isRepay(row) {
      crudRepay.edit(row).then(res => {
        this.$message.success('还款成功')
      })
    },
    beforeupload(file) {
      const isLt1M = file.size / 1024 / 1024 < 100
      if (!isLt1M) {
        this.$message.error('上传文件大小不能超过 100MB!')
      }
      this.fileName = file.name
      return isLt1M
    },
    /** 导入模板的成功状态 */
    handleSuccessFile(res, file) {
      if (res.state === 'success') {
        this.$message({
          message: res.info,
          type: 'success'
        })
        this.crud.toQuery()
      } else {
        this.$message.error(res.message)
      }
    },
    /** 失败状态 */
    handleErrorFile(file) {
      this.$message.error('数据导入失败！')
    },
    /** 下载模板 */
    downloadModel() {
      var params = { }
      params.id = -1
      download(this.crud.url + '/export', params).then(result => {
        downloadFile(result, this.crud.title + '模板', 'xlsx')
      })
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
